/**
 * 
 */
package com.gmail.blackdog1987.carcare.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.gmail.blackdog1987.carcare.dao.CarChangeShopDao;
import com.gmail.blackdog1987.carcare.dao.CarChangeShopDetailDao;
import com.gmail.blackdog1987.carcare.dao.CarFixShopDao;
import com.gmail.blackdog1987.carcare.dao.CarRentDao;
import com.gmail.blackdog1987.carcare.dao.CarSaleDao;
import com.gmail.blackdog1987.carcare.dao.CleanShopDao;
import com.gmail.blackdog1987.carcare.dao.ConfigDao;
import com.gmail.blackdog1987.carcare.dao.DriveSchoolDao;
import com.gmail.blackdog1987.carcare.dao.MaintainDao;
import com.gmail.blackdog1987.carcare.dao.MaintainDetailDao;
import com.gmail.blackdog1987.carcare.dao.OrderCarChangeDao;
import com.gmail.blackdog1987.carcare.dao.OrderCarChangeDetailDao;
import com.gmail.blackdog1987.carcare.dao.OrderCarCleanDao;
import com.gmail.blackdog1987.carcare.dao.OrderCarFixDao;
import com.gmail.blackdog1987.carcare.dao.OrderCarRentDao;
import com.gmail.blackdog1987.carcare.dao.OrderDriveCompanyDao;
import com.gmail.blackdog1987.carcare.dao.OrderDriveSchoolDao;
import com.gmail.blackdog1987.carcare.dao.OrderMaintainDao;
import com.gmail.blackdog1987.carcare.dao.OrderMaintainDetailDao;
import com.gmail.blackdog1987.carcare.dao.OrderRoadHelpDao;
import com.gmail.blackdog1987.carcare.dao.OrderRulesCheckDao;
import com.gmail.blackdog1987.carcare.dao.ProductDao;
import com.gmail.blackdog1987.carcare.dao.hibernate.jdbc.BaseJdbcDao;
import com.gmail.blackdog1987.carcare.dao.hibernate.jdbc.JdbcUtil;
import com.gmail.blackdog1987.carcare.entity.TCarRent;
import com.gmail.blackdog1987.carcare.entity.TCarSale;
import com.gmail.blackdog1987.carcare.entity.TCarchangeshop;
import com.gmail.blackdog1987.carcare.entity.TCarchangeshopDetail;
import com.gmail.blackdog1987.carcare.entity.TCarfixshop;
import com.gmail.blackdog1987.carcare.entity.TCleanshop;
import com.gmail.blackdog1987.carcare.entity.TConfig;
import com.gmail.blackdog1987.carcare.entity.TMaintain;
import com.gmail.blackdog1987.carcare.entity.TMaintainDetail;
import com.gmail.blackdog1987.carcare.entity.TOrderCarchange;
import com.gmail.blackdog1987.carcare.entity.TOrderCarchangeDetail;
import com.gmail.blackdog1987.carcare.entity.TOrderCarfix;
import com.gmail.blackdog1987.carcare.entity.TOrderCarrent;
import com.gmail.blackdog1987.carcare.entity.TOrderCarsale;
import com.gmail.blackdog1987.carcare.entity.TOrderClean;
import com.gmail.blackdog1987.carcare.entity.TOrderDrivecompany;
import com.gmail.blackdog1987.carcare.entity.TOrderDriveschool;
import com.gmail.blackdog1987.carcare.entity.TOrderMaintain;
import com.gmail.blackdog1987.carcare.entity.TOrderMaintainDetail;
import com.gmail.blackdog1987.carcare.entity.TOrderRoadhelp;
import com.gmail.blackdog1987.carcare.entity.TOrderRulesCheck;
import com.gmail.blackdog1987.carcare.entity.TProduct;
import com.gmail.blackdog1987.carcare.service.OrderService;
import com.gmail.blackdog1987.carcare.util.ErrorCode;
import com.gmail.blackdog1987.carcare.util.NullUtil;

/**
 * 
 * @author 董国超
 *
 */
@Service("orderService")
public class OrderServiceImpl implements OrderService {
	
	@Autowired
	private OrderMaintainDao orderMaintainDao;
	@Autowired
	private OrderMaintainDetailDao orderMaintainDetailDao;
	@Autowired
	private MaintainDetailDao maintainDetailDao;
	@Autowired
	private MaintainDao maintainDao;
	@Autowired
	private	OrderDriveCompanyDao orderDriveCompanyDao;
	@Autowired
	private	OrderDriveSchoolDao orderDriveSchoolDao;
	@Autowired
	private	OrderCarRentDao orderCarRentDao;
	@Autowired
	private	OrderRoadHelpDao orderRoadHelpDao;
	@Autowired
	private	OrderCarFixDao orderCarFixDao;
	@Autowired
	private	OrderCarChangeDao orderCarChangeDao;
	@Autowired
	private	OrderCarChangeDetailDao orderCarChangeDetailDao;
	@Autowired
	private	CarRentDao carRentDao;
	@Autowired
	private	CarSaleDao carSaleDao;
	@Autowired
	private	CarChangeShopDao carChangeShopDao;
	@Autowired
	private	CarChangeShopDetailDao carChangeShopDetailDao;
	@Autowired
	private	CarFixShopDao carFixShopDao;
	@Autowired
	private	OrderCarCleanDao orderCarCleanDao;
	@Autowired
	private	OrderRulesCheckDao orderRulesCheckDao;
	@Autowired
	private BaseJdbcDao baseJdbcDao;
	@Autowired
	private ProductDao productDao;
	@Autowired
	private CleanShopDao cleanShopDao;
	@Autowired
	private DriveSchoolDao driveSchoolDao;
	@Autowired
	private ConfigDao configDao;

	@Override
	public Map<String, Object> saveOrderMaintain(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		result.put("code", ErrorCode.NO);
		
		String maintainid = filter.get("maintainid");
		String detailid = filter.get("detailid");
		String userid = filter.get("userid");
		
		TOrderMaintain orderMaintain = new TOrderMaintain();
		orderMaintain.setUserId(Integer.valueOf(userid));
		orderMaintain.setMaintainId(Integer.valueOf(maintainid));
		
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", maintainid);
		List<TMaintain> maintains = maintainDao.find(param);
		// 算总价
		String[] detailIds = null;
		if(detailid!=null&&detailid.length()>0){
			detailIds = detailid.split(",");
		}
		// 全部的detail查出来再遍历
		List<TMaintainDetail> maintainDetails = JdbcUtil.queryForList(baseJdbcDao, TMaintainDetail.class, " select * from t_maintain_detail where maintain_id =?", new Object[]{maintainid});
		double totalprice = 0;
		for (TMaintainDetail tMaintainDetail : maintainDetails) {
			for (String id : detailIds) {
				if(tMaintainDetail.getId().intValue() == Integer.valueOf(id).intValue()){	// 判断下单中选择了哪些detail
					totalprice += tMaintainDetail.getPrice();
				}
			}
		}
		orderMaintain.setTotalprice(totalprice);
		// 维护冗余字段
		if(maintains!=null&&maintains.size()>0){
			orderMaintain.setType(maintains.get(0).getType());
		}
		Serializable id = orderMaintainDao.create(orderMaintain);
		orderMaintain.setId(Integer.valueOf(id.toString()));
		
		// 订单扩展表
		if(detailIds!=null&&detailIds.length>0){
			List<TOrderMaintainDetail> details = new ArrayList<TOrderMaintainDetail>();
			for (String detaiId : detailIds) {
				if(detaiId!=null&&detaiId.length()>0){
					TOrderMaintainDetail detail = new TOrderMaintainDetail();
					detail.setDetailId(Integer.valueOf(detaiId.trim()));
					detail.setOrderId(orderMaintain.getId());
					details.add(detail);
				}
			}
			orderMaintainDetailDao.saveAll(details);
		}
		TProduct product = new TProduct();
		product.setCreatetime(new Date());
		product.setPrice(Double.valueOf(orderMaintain.getTotalprice()));
		product.setSubject("");
		product.setTableName("t_order_maintain");
		product.setTableId(orderMaintain.getId());
		product.setUserid(Integer.valueOf(userid));
		product.setBody("洗车保养:");
		if(product.getPrice()<=0){
			product.setStatus(1);
		}
		productDao.save(product);
		result.put("result", orderMaintain);
		return result;
	}

	@Override
	public Map<String, Object> saveOrderDriveCompany(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		result.put("code", ErrorCode.NO);
		
		String name = filter.get("name");
		String phone = filter.get("phone");
		String startpoint = filter.get("startpoint");
		String time = filter.get("time");
		String endpoint = filter.get("endpoint");
		String userid = filter.get("userid");
		
		TOrderDrivecompany order = new TOrderDrivecompany();
		order.setName(name==null?"":name);
		order.setPhone(phone==null?"":phone);
		order.setUserId(Integer.valueOf(userid));
		order.setStartpoint(startpoint);
		order.setEndpoint(endpoint);
		if(time==null||time.length()==0){
		}else{
			order.setTime(new Date(Long.valueOf(time)));
		}
		
		JSONObject priceObj = null;
		TConfig config = configDao.getUniqueBy("key", "price");
		if(config!=null){
			String value = config.getValue();
			if(value!=null&&value.length()!=0){
				priceObj = JSONObject.parseObject(value);
			}
		}
		
		Serializable id = orderDriveCompanyDao.create(order);
		order.setId((Integer) id);
		
		TProduct product = new TProduct();
		product.setCreatetime(new Date());
		product.setStatus(0);
		product.setPrice(Double.valueOf(priceObj.getInteger("drivecompany")));
		product.setSubject("车拉丁");
		product.setTableName("t_order_drivecompany");
		product.setTableId(order.getId());
		product.setUserid(Integer.valueOf(userid));
		product.setBody("驾校陪驾");
		if(product.getPrice()<=0){
			product.setStatus(1);
		}
		productDao.save(product);
		
		result.put("result", order);
		return result;
	}

	@Override
	public Map<String, Object> saveOrderCarRent(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		result.put("code", ErrorCode.NO);
		
		String userid = filter.get("userid");
		String days = filter.get("days");
		String carid = filter.get("carid");
		String name = filter.get("name");
		String phone = filter.get("phone");
		String info = filter.get("info");
		
		TOrderCarrent order = new TOrderCarrent();
		
		TCarRent car = carRentDao.get(Integer.valueOf(carid));
		if(car==null){
			// 异常
			return result;
		}
		order.setName(name);
		order.setPhone(phone);
		order.setInfo(info);
		order.setCarId(car.getId());
		order.setDays(Integer.valueOf(days));
		order.setPrice(car.getPrice());
		order.setTotalprice(car.getPrice()*Integer.valueOf(days));
		order.setUserId(Integer.valueOf(userid));
		Serializable id = orderCarRentDao.create(order);
		order.setId((Integer) id);
		TProduct product = new TProduct();
		product.setCreatetime(new Date());
		product.setStatus(0);
		product.setPrice(Double.valueOf(order.getTotalprice()));
		product.setSubject("");
		product.setTableName("t_order_carrent");
		product.setTableId(order.getId());
		product.setUserid(Integer.valueOf(userid));
		product.setBody("租车:");
		if(product.getPrice()<=0){
			product.setStatus(1);
		}
		productDao.save(product);
		result.put("result", order);
		return result;
	}

	@Override
	public Map<String, Object> saveOrderCarSale(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		result.put("code", ErrorCode.NO);
		
		String userid = filter.get("userid");
		String carid = filter.get("carid");
		String name = filter.get("name");
		String phone = filter.get("phone");
		String info = filter.get("info");
		
		TOrderCarsale order = new TOrderCarsale();
		
		TCarSale car = carSaleDao.get(Integer.valueOf(carid));
		if(car==null){
			// 异常
			return result;
		}
		order.setCarId(car.getId());
		order.setInfo(info==null?"":info);
		order.setPhone(phone==null?"":phone);
		order.setPrice(order.getPrice());
		order.setUsername(name==null?"":name);
		order.setUserId(Integer.valueOf(userid));
		Serializable id = orderCarRentDao.create(order);
		order.setId((Integer) id);
		result.put("result", order);
		return result;
	}

	@Override
	public Map<String, Object> saveOrderClean(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		result.put("code", ErrorCode.NO);
		
		String userid = filter.get("userid");
		String shopid = filter.get("shopid");
		String license = filter.get("license");
		
		TCleanshop shop = cleanShopDao.get(Integer.valueOf(shopid));
		TOrderClean order = new TOrderClean();
		order.setUserId(Integer.valueOf(userid));
		order.setShopid(Integer.valueOf(shopid));
		order.setPrice(Double.valueOf(shop.getPrice()));
		order.setLicense(license);
		Serializable id = orderCarCleanDao.create(order);
		order.setId((Integer) id);
		
		TProduct product = new TProduct();
		product.setCreatetime(new Date());
		product.setPrice(Double.valueOf(order.getPrice()));
		product.setSubject(shop.getTitle());
		product.setTableName("t_order_clean");
		product.setTableId(order.getId());
		product.setUserid(Integer.valueOf(userid));
		product.setBody("洗车:车牌号"+license);
		if(product.getPrice()<=0){
			product.setStatus(1);
		}
		productDao.save(product);
		
		result.put("result", order);
		return result;
	}

	@Override
	public Map<String, Object> saveOrderRoadHelp(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		result.put("code", ErrorCode.NO);
		
		String userid = filter.get("userid");
		String name = filter.get("name");
		String phone = filter.get("phone");
		String info = filter.get("info");
		String type = filter.get("type");
		String location = filter.get("location");
		String lat = filter.get("lat");
		String lon = filter.get("lon");
		
		TOrderRoadhelp order = new TOrderRoadhelp();
		
		order.setLat(NullUtil.isEmpty(lat)?0.0:Double.valueOf(lat));
		order.setLon(NullUtil.isEmpty(lon)?0.0:Double.valueOf(lon));
		order.setLocation(location==null?"":location);
		order.setType(NullUtil.isEmpty(type)?1:Integer.valueOf(type));
		order.setInfo(info==null?"":info);
		order.setPhone(phone==null?"":phone);
		order.setUsername(name==null?"":name);
		order.setUserId(Integer.valueOf(userid));
		Serializable id = orderRoadHelpDao.create(order);
		order.setId((Integer) id);
		
		JSONObject priceObj = null;
		TConfig config = configDao.getUniqueBy("key", "price");
		if(config!=null){
			String value = config.getValue();
			if(value!=null&&value.length()!=0){
				priceObj = JSONObject.parseObject(value);
			}
		}
		
		TProduct product = new TProduct();
		product.setCreatetime(new Date());
		product.setStatus(0);
		product.setPrice(Double.valueOf(priceObj.getInteger("roadhelp")));
		product.setSubject("车拉丁");
		product.setTableName("t_order_roadhelp");
		product.setTableId(order.getId());
		product.setUserid(Integer.valueOf(userid));
		product.setBody("道路救援");
		if(product.getPrice()<=0){
			product.setStatus(1);
		}
		productDao.save(product);
		result.put("result", order);
		return result;
	}

	@Override
	public Map<String, Object> saveOrderRulesCheck(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		result.put("code", ErrorCode.NO);
		
		String userid = filter.get("userid");
		String name = filter.get("name");
		String phone = filter.get("phone");
		String info = filter.get("info");
		String type = filter.get("type");
		
		TOrderRulesCheck order = new TOrderRulesCheck();
		
		order.setInfo(info==null?"":info);
		order.setPhone(phone==null?"":phone);
		order.setUsername(name==null?"":name);
		order.setType(NullUtil.isEmpty(type)?1:Integer.valueOf(type));
		order.setUserId(Integer.valueOf(userid));
		Serializable id = orderRulesCheckDao.create(order);
		order.setId((Integer) id);
		
		JSONObject priceObj = null;
		TConfig config = configDao.getUniqueBy("key", "price");
		if(config!=null){
			String value = config.getValue();
			if(value!=null&&value.length()!=0){
				priceObj = JSONObject.parseObject(value);
			}
		}
		
		TProduct product = new TProduct();
		product.setStatus(0);
		product.setCreatetime(new Date());
		product.setPrice(Double.valueOf(priceObj.getInteger("license")));
		product.setSubject("车拉丁");
		product.setTableName("t_order_rulescheck");
		product.setTableId(order.getId());
		product.setUserid(Integer.valueOf(userid));
		product.setBody("违章年审");
		if(product.getPrice()<=0){
			product.setStatus(1);
		}
		productDao.save(product);
		
		result.put("result", order);
		return result;
	}

	@Override
	public Map<String, Object> saveOrderCarFix(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		result.put("code", ErrorCode.NO);
		
		String userid = filter.get("userid");
		String name = filter.get("name");
		String phone = filter.get("phone");
		String info = filter.get("info");
		String shopid = filter.get("shopid");
		String time = filter.get("time");
		
		TOrderCarfix order = new TOrderCarfix();
		Date date = new Date();
		TCarfixshop shop = carFixShopDao.get(Integer.valueOf(shopid));
		
		if(time==null||time.length()==0){
			
		}else{
			date = new Date(Long.valueOf(time));
		}
		order.setTime(date);
		order.setInfo(info==null?"":info);
		order.setPhone(phone==null?"":phone);
		order.setUsername(name==null?"":name); 
		order.setUserId(Integer.valueOf(userid));
		order.setShopid(Integer.valueOf(shopid));
		Serializable id = orderCarChangeDao.create(order);
		order.setId((Integer) id);
		
		TProduct product = new TProduct();
		product.setCreatetime(new Date());
		product.setStatus(0);
		product.setPrice(Double.valueOf(shop.getPrice()));
		product.setSubject("车拉丁");
		product.setTableName("t_order_carfix");
		product.setTableId(order.getId());
		product.setUserid(Integer.valueOf(userid));
		product.setBody("车辆维修");
		if(product.getPrice()<=0){
			product.setStatus(1);
		}
		productDao.save(product);
		
		result.put("result", order);
		return result;
	}
	
	@Override
	public Map<String, Object> saveOrderCarChange(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		result.put("code", ErrorCode.NO);
		
		String userid = filter.get("userid");
		String name = filter.get("name");
		String phone = filter.get("phone");
		String info = filter.get("info");
		String shopid = filter.get("shopid");
		String detailid = filter.get("detailid");
		
		TOrderCarchange change = new TOrderCarchange();
		change.setUserId(Integer.valueOf(userid));
		change.setShopid(Integer.valueOf(shopid));
		change.setInfo(info);
		change.setUsername(name);
		change.setPhone(phone);
		
		TCarchangeshop shop = carChangeShopDao.get(Integer.valueOf(shopid));
		// 算总价
		String[] detailIds = null;
		if(detailid!=null&&detailid.length()>0){
			detailIds = detailid.split(",");
		}
		// 全部的detail查出来再遍历
		List<TCarchangeshopDetail> changeDetail = JdbcUtil.queryForList(baseJdbcDao, TCarchangeshopDetail.class, " select * from t_carchangeshop_detail where shopid =?", new Object[]{shopid});
		double totalprice = 0;
		for (TCarchangeshopDetail detail : changeDetail) {
			for (String id : detailIds) {
				if(detail.getId().intValue() == Integer.valueOf(id).intValue()){	// 判断下单中选择了哪些detail
					totalprice += detail.getPrice();
				}
			}
		}
		change.setPrice(totalprice);
		change.setType(shop.getType());
		Serializable id = orderCarChangeDao.create(change);
		change.setId(Integer.valueOf(id.toString()));
		
		// 订单扩展表
		if(detailIds!=null&&detailIds.length>0){
			List<TOrderCarchangeDetail> details = new ArrayList<TOrderCarchangeDetail>();
			for (String detaiId : detailIds) {
				if(detaiId!=null&&detaiId.length()>0){
					TOrderCarchangeDetail detail = new TOrderCarchangeDetail();
					detail.setDetailId(Integer.valueOf(detaiId.trim()));
					detail.setOrderId(change.getId());
					details.add(detail);
				}
			}
			orderCarChangeDetailDao.saveAll(details);
		}
		TProduct product = new TProduct();
		product.setCreatetime(new Date());
		product.setPrice(change.getPrice());
		product.setSubject(shop.getTitle());
		product.setTableName("t_order_carchange");
		product.setTableId(change.getId());
		product.setUserid(Integer.valueOf(userid));
		product.setBody("配饰改装");
		if(product.getPrice()<=0){
			product.setStatus(1);
		}
		productDao.save(product);
		result.put("result", change);
		return result;
	
	}
	
	
	@Override
	public Map<String, Object> saveOrderDriveSchool(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		result.put("code", ErrorCode.NO);
		
		String userid = filter.get("userid");
		String schoolid = filter.get("schoolid");
		String name = filter.get("name");
		String phone = filter.get("phone");
		String info = filter.get("info");
		
		TOrderDriveschool order = new TOrderDriveschool();
		order.setUserid(Integer.valueOf(userid));
		order.setSchoolid(Integer.valueOf(schoolid));
		order.setName(name);
		order.setPhone(phone);
		order.setInfo(info==null?"":info);
		Serializable id = orderCarCleanDao.create(order);
		order.setId((Integer) id);
		
		result.put("result", order);
		return result;
	}
}
